package com.jin.webvideo.controller;

import com.jin.webvideo.entity.Page;
import com.jin.webvideo.exception.ParsingFailedException;
import com.jin.webvideo.externalInterfaces.PlatformSupport;
import com.jin.webvideo.externalInterfaces.entity.Filmography;
import com.jin.webvideo.entity.ResultEntity;
import com.jin.webvideo.parser.ParseResult;
import com.jin.webvideo.service.*;
import com.jin.webvideo.util.base.MapBuilder;
import com.jin.webvideo.util.base.MapUtil;
import com.jin.webvideo.util.base.StringUtil;
import com.jin.webvideo.util.base.URLUtil;
import com.jin.webvideo.util.sys.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/api")
public class InterfaceController {
    @Autowired
    private ApiService apiService;
    @Autowired
    private VideoService videoService;
    @Autowired
    private ConfigService configService;
    @Autowired
    private ParserService parserService;

    @ResponseBody
    @GetMapping("/notice")
    public ResultEntity notice(){
        return ResultEntity.success((Object) configService.notice());
    }

    @GetMapping("/s")
    @ResponseBody
    public ResultEntity search(@RequestParam(required = false, defaultValue = "iqiyi")String platform, String key, @RequestParam(required = false) List<String> channel, @RequestParam(required = false, defaultValue = "1") int page, @RequestParam(required = false, defaultValue = "20")int pageSize) throws Exception {
        key = URLUtil.encodeURL(key);
        Page<Filmography> filmographyList = apiService.search(platform, key, channel, page, pageSize);
        videoService.addFilmographys(filmographyList.getList());
        return ResultEntity.success(filmographyList);
    }

    @GetMapping("/suggest")
    @ResponseBody
    public ResultEntity suggest(@RequestParam(required = true, defaultValue = "") String key, String no) throws Exception {
        key = URLUtil.encodeURL(key);
        List<String> list = apiService.suggest(PlatformSupport.PLATFORM_IQIYI, key, 5);
        Map<String,Object> map = new HashMap<>();
        map.put("list", list);
        map.put("no", no);
        map.put("key", key);
        return ResultEntity.success(map);
    }

    @ResponseBody
    @PostMapping("/url")
    public ResultEntity url(HttpServletRequest request, @RequestParam(required = true) String data, @RequestParam(required = false, defaultValue = "")String platform, @RequestParam(required = false, defaultValue = "")String name) throws IOException, ParsingFailedException {
        String url = EncryptUtil.base64Decrypt(data);
        if (StringUtil.isEmpty(platform)){
            platform = PlatformSupport.getPlatform(url);
        }
        ParseResult result = parserService.parseRealUrl(request, platform, url, name);
        String relurl = result.getUrl();
        MapBuilder<String,String> builder = new MapBuilder<>();
        Map<String,String> map = builder.putAll(MapUtil.toMap(result))
                .put("preloaded", "30").build();
        if (relurl.startsWith("http")){
            map.put("preloaded", "5400");
        }
        return ResultEntity.success(map);
    }

}
